define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'subsidiary/index' + location.search,
                    add_url: 'subsidiary/add',
                    edit_url: 'subsidiary/edit',
                    del_url: 'subsidiary/del',
                    multi_url: 'subsidiary/multi',
                    import_url: 'subsidiary/import',
                    table: 'admin',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { field: 'state', checkbox: true, },
                        { field: 'id', title: 'ID' },
                        { field: 'pid', title: __('城市公司'), searchList:Config.admin_lists, formatter: Table.api.formatter.normal },
                        { field: 'username', title: __('Username') },
                        { field: 'nickname', title: __('Nickname') },
                        // {field: 'groups_text', title: __('Group'), operate:false, formatter: Table.api.formatter.label},
                        { field: 'region', title: __('负责城市') },
                        {field: 'name', title: __('负责人')},
                        { field: 'mobile', title: __('Mobile') },
                        { field: 'status', title: __("Status"), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        { field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                if (row.id == Config.admin.id) {
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        dashboard: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'subsidiary/dashboard' + location.search,
                    table: 'admin',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'total_turnover',
                sortOrder: 'desc',
                search: false,
                showToggle: false,
                showExport: false,
                showColumns: false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { field: "rank", title: __("Rank"), width: "30%", operate: false },
                        { field: "nickname", title: __("Nickname")},
                        { field: "total_turnover", title: __("Total_turnover"), width: "30%", operate: false },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $(document).on("change", "#c-region_id", function (row, obj) {
                $('#c-address').selectPageClear();
            });

            $("#c-address").data("params", function (obj) {
                return { custom: { pid: $("#c-region_id").val() } };
            })
            Controller.api.bindevent();
        },
        edit: function () {

            $(document).on("change", "#c-region_id", function (row, obj) {
                $('#c-address').selectPageClear();
            });

            $("#c-address").data("params", function (obj) {
                return { custom: { pid: $("#c-region_id").val() } };
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
